import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_ui/src/theme/colors.dart';
import 'package:flutter_ui/src/theme/sizes.dart';

TextStyle defaultTitleStyle = TextStyle(color: TitleColor, fontSize: DefaultFontSize, fontWeight: FontWeight.w400);
TextStyle defaultSubtitleStyle = TextStyle(color: DescColor, fontSize: SmallFontSize, fontWeight: FontWeight.w400);
EdgeInsetsGeometry defaultPadding = EdgeInsets.symmetric(vertical: DefaultMargin, horizontal: LargeMargin);

class ListItem extends StatelessWidget {
  final String title;
  final String subtitle;
  final IconData icon;
  final bool arrow;
  final Widget child;
  final TextStyle titleStyle;
  final TextStyle subtitleStyle;
  final EdgeInsetsGeometry margin;
  final EdgeInsetsGeometry padding;
  final VoidCallback onTap;
  final VoidCallback onLongPress;
  final bool horizontal;

  ListItem({Key key, this.title = "", this.subtitle = "", this.arrow = false, this.child, this.icon, this.titleStyle, this.subtitleStyle, this.margin, this.padding, this.onTap, this.onLongPress, this.horizontal = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    TextStyle _titleStyle = defaultTitleStyle.merge(this.titleStyle);
    TextStyle _subtitleStyle = defaultSubtitleStyle.merge(this.subtitleStyle);
    bool noTitle = title == null || title.isEmpty;
    bool noSubtitle = subtitle == null || subtitle.isEmpty;

    Column titleColumn = Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        noTitle ? null : Text(title, style: _titleStyle),
        noSubtitle ? null: Text(subtitle, style: _subtitleStyle),
      ].where((item)=> item != null).toList(),
    );
    List<Widget> rowChildren = <Widget>[
      icon!=null ? Icon(icon) : null,
      noTitle && noSubtitle ? null : titleColumn,
      child != null ? Expanded(
        flex: 1,
        child: child,
      ) : null,
      arrow ? Container(
        transform: Matrix4.identity()..translate(6.0, 0.0, 0.0),
        child: Icon(CupertinoIcons.right_chevron, color: DescColor,),
      ) : null
    ];

    return
      InkWell(
        onTap: this.onTap,
        onLongPress: this.onLongPress,
        child: Container(
          padding: padding == null ? defaultPadding : padding,
          margin: margin,
          child: horizontal ? Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: rowChildren.where((item)=> item!=null).toList(),
          ) : Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: rowChildren.where((item)=> item!=null).toList(),
          )
      )
    );
  }
}